Program to Read the keys from a Table
import java.sql.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
Statement st=con.createStatement();
st.executeUpdate(“create table test(eno number(5) primary key,
Na varchar2(20),sal number(5))”);
st.executeUpdate(“insert into test values(100,’prasad’,3430)”);
ResultSet rs=null;
DatabaseMetaData meta=con.getMetaData();
rs=meta.getPrimaryKeys(null,null,”test”);
while(rs.next())
{
System.out.println(rs.getString(“COLUMN_NAME”));
}
rs.close();
st.close();
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
Program to Display the views in Database
import java.sql.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
Statement st=con.createStatement();
st.executeUpdate(“create table test(eno number(5) primary key,
Na varchar2(20),sal number(5))”);
st.executeUpdate(“insert into test values(100,’prasad’,3430)”);
st.executeUpdate(“create view myview as select * from test”);
ResultSet rs=null;
DatabaseMetaData meta=con.getMetaData();
rs=meta.getTables(null,null,null,new String[]{“VIEW”});
while(rs.next())
{
System.out.println(rs.getString(“TABLE_NAME”));
}
rs.close();
st.close();
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}